package wzh000;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class CloudinessCalculator {
	
	private String req;

	public CloudinessCalculator(DateReq item){
		//this.req = "http://vvnas00:9909/"+item.getYear()+"/"+item.getMonth()+"/"+item.getDay()+"/wcam0_"
		//		+item.getYearMonthDay()+"_"+item.getHourMin()+".jpg";
		this.req = "http://127.0.0.1:8080/HTTPServer/"+item.getDateTime()+".jpg";
	}
	
	public double getCloudiness(){
		BufferedImage img = new BufferedImage(626, 266, BufferedImage.TYPE_INT_RGB);
		System.out.println("type:" + img.getType());
		int x, y;
		int r=0, g=0, b=0;
		int Nbw = 0;
		int N = 0;
		int Nbright = 0;
		int p;
		double CC = 0;

	    try {
	    	//String reqToImageSrv = "http://vvnas00:9909/2003/06/09/wcam0_20030609_0015.jpg";
	    	URL imgServerURL = new URL(this.req);
	        img = ImageIO.read(imgServerURL);
	        int w = img.getWidth();
	        int h = img.getHeight();

	        //int[] dataBuffInt = img.getRGB(0, 0, w, h, null, 0, w); 

	        //Color c = new Color(dataBuffInt[100]);
	        /*
	        System.out.println(c.getRed());   // = (dataBuffInt[100] >> 16) & 0xFF
	        System.out.println(c.getGreen()); // = (dataBuffInt[100] >> 8)  & 0xFF
	        System.out.println(c.getBlue());  // = (dataBuffInt[100] >> 0)  & 0xFF
	        System.out.println(c.getAlpha()); // = (dataBuffInt[100] >> 24) & 0xFF
	        */
	        int xx, yy;
	        System.out.println("type:" + img.getType());
	        for (x=0; x<626; x++){
	            for (y=0; y<266; y++){
	                
	            	p = img.getRGB(x, y);
	            //	 System.out.println("type:" + img.getType());
	            	Color c = new Color(p);
	            	//Color c = new Color(img.getRGB(x,y));
	            	// System.out.println("type:" + img.getType());
	            	//p = 8288377;
	                //p = getpixel( image, x, y );
	                // p is 32 bit unsigned int with the RGB values in 8+8+8 bits
	            	r =  c.getRed();
	            	g = c.getGreen();
	            	b =  c.getBlue();
	            	/*
	                r = (short) (p & 255);
	                g = (short) ((p>>8)&255);
	                b = (short) ((p>>16)&255);
	                */
	            	
	                
	            	
	            	//System.out.println("x:"+x+"y:"+y+"r:"+r+",g:"+g+",b;"+b);
	                
	                if( ( r + b + g ) > 160 ) Nbright += 1;
	                
	                if ( b > ( (r + g) / 1.9 ) )  
	                    Nbw += 1;
	                else
	                    N += 1;
	            }
	        }
	        
	        if ( Nbright > ( 626*266 )*0.4 )
	            CC = 100.0 - Nbw*100.0/(626*266);
	        else
	            CC = 0.0;
	        System.out.format("CC: %.1f%%", CC);
	        

	    }catch(IOException e){
	    	e.printStackTrace();
	    }
	    return CC;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
